require "rubygems"
require "capistrano"

module Wondercap

  class MultiConfiguration
    include Enumerable
    
    attr_accessor :extension_modules, :initializers
    
    def initialize(extension_modules = [], initializers = [])
      @configurations = {}
      @extension_modules = extension_modules
      @initializers = initializers
    end
    
    def get_configuration(name)
      @configurations[name.to_sym] ||= instance_eval do
        config = Capistrano::Configuration.new
        @extension_modules.each do |mod| 
          class << config
            self
          end.instance_eval do
            include mod
          end
        end
        @initializers.each do |initializer|
          initializer.call(name, config)
        end
        config
      end
    end
    
    def find_configuration(name)
      get_configuration(name) if is_defined?(name)
    end
    
    def is_defined?(name)
      @configurations.key?(name.to_sym)
    end
    
    def each(&block) 
      @configurations.each(&block)
    end
  end
  
end
